package com.xdpdp.smile.cinema.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.xdpdp.smile.cinema.controller.vo.CinemaAddReqVO;
import com.xdpdp.smile.cinema.dao.entity.MoocCinemaT;
import com.xdpdp.smile.cinema.service.CinemaServiceAPI;
import com.xdpdp.smile.utils.common.vo.BaseResponseVO;
import com.xdpdp.smile.utils.exception.CommonServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Herman
 * @description : 影院相关的表现层
 **/
@RestController
@RequestMapping("/cinemas")
public class CinemaController {

    @Autowired
    private CinemaServiceAPI cinemaServiceAPI;

    @RequestMapping(value = "/cinema:add", method = RequestMethod.POST)
    public BaseResponseVO addCinema(@RequestBody CinemaAddReqVO cinemaAddReqVO) {
        BaseResponseVO baseResponseVO = null;
        try {
            cinemaAddReqVO.checkParam();
            cinemaServiceAPI.addCinema(cinemaAddReqVO);

            baseResponseVO = BaseResponseVO.success();
        } catch (CommonServiceException e) {
            baseResponseVO = BaseResponseVO.serviceException(e);
        }
        return baseResponseVO;
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public BaseResponseVO describeCinemas(
            @RequestParam(name = "nowPage", defaultValue = "1") int nowPage,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        BaseResponseVO baseResponseVO = null;
        try {

            IPage<MoocCinemaT> page = cinemaServiceAPI.describeCinemas(nowPage, pageSize);
            Map<String, Object> result = Maps.newHashMap();
            result.put("nowPage", page.getCurrent());
            result.put("pageSize", page.getSize());
            result.put("totalPages", page.getPages());
            result.put("totalSize", page.getTotal());

            if (page.getSize() == 0) {
                baseResponseVO = BaseResponseVO.success();
            } else {
                List<CinemaAddReqVO> cinemas = new ArrayList<CinemaAddReqVO>();
                List<MoocCinemaT> records = page.getRecords();
                for (MoocCinemaT moocCinemaT : records) {
                    CinemaAddReqVO cinemaAddReqVO = new CinemaAddReqVO();
                    cinemaAddReqVO.setBrandId(moocCinemaT.getBrandId());
                    cinemaAddReqVO.setAreaId(moocCinemaT.getAreaId());
                    cinemaAddReqVO.setHallTypeIds(moocCinemaT.getHallIds());
                    cinemaAddReqVO.setCinemaName(moocCinemaT.getCinemaName());
                    cinemaAddReqVO.setCinemaAddress(moocCinemaT.getCinemaAddress());
                    cinemaAddReqVO.setCinemaTele(moocCinemaT.getCinemaPhone());
                    cinemaAddReqVO.setCinemaImgAddress(moocCinemaT.getImgAddress());
                    cinemaAddReqVO.setCinemaPrice(moocCinemaT.getMinimumPrice());
                    cinemas.add(cinemaAddReqVO);
                }
                result.put("cinemas", cinemas);
                baseResponseVO = BaseResponseVO.success(result);
            }
        } catch (CommonServiceException e) {
            baseResponseVO = BaseResponseVO.serviceException(e);
        }
        return baseResponseVO;
    }

}